<?php 
if(!defined('BASEPATH'))
		exit('No direct script access allowed');
	class Topstories extends CI_Controller {
		function __construct(){
			session_start();
			parent::__construct();
		}
		
		function index($query_id=0, $order_by  = 'created_date', $sort_order = 'asc', $current_page = 0){
			
			$search_by='';
			$search_by_value='';
				if($query_id == '0'){		
					//echo '<pre>';print_r($_SESSION);exit;
					unset($_SESSION['search']);
				}else{
					if(isset($_SESSION['search'])){
					$array = $_SESSION['search'];
					//echo '<pre>';print_r($_SESSION);exit;
					$search_by = $array['search_by'];
					$search_by_value = $array['search'];
					}
				}
				
			$results_per_page = 3;
		
		
			$url = CURL_URL;
			$data = array('www-command'=>'topstories-all_topstories', 'current_page'=>$current_page, 'results_per_page'=>$results_per_page,'sort_order'=>$sort_order,'order_by'=>$order_by,'search_by'=>$search_by,'search_by_value'=>$search_by_value);
			
			$string = http_build_query($data);
			//echo '<pre>';print_r($data);exit;
			$ch = curl_init($url);
			curl_setopt($ch,CURLOPT_POST,true);
			curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
			$response= json_decode(curl_exec($ch));
			if($response->message !== 'No Top stories'){
			//echo '<pre>';print_r($response);exit;
			curl_close($ch);
		
			$this->load->library('pagination');			
			$config['base_url'] = base_url().'index.php/admin/topstories/index/'.$query_id.'/'.$order_by.'/'.$sort_order;
			$config['per_page'] = $results_per_page;
			$config['total_rows']  = $response->total->total;
			$config['uri_segment'] = 7;
			$config['num_links'] = 1;
			$config['prev_link'] = 'Previous';
			$config['next_link'] = 'Next';
			$config['first_link'] = 'First';
			$config['last_link'] = 'Last';
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
			$data['topstories_all_topstories'] = $response;
			$data['topstory'] = '';
			}else{
				$data['topstory'] = 1;
				$data['topstories_all_topstories'] = '';
			}
			//echo '<pre>';print_r($data);exit;
			$data['topstories_fileds'] = array('topstories_title'=>'Title','created_date'=>'Date', 'topstories_author'=>'Author','status'=>'Status');
			$data['order_by'] = $order_by;
			$data['sort_order'] = $sort_order;
			$data['query_id'] = $query_id;
			$data['search_by'] = $search_by;
			$data['search_by_value'] = $search_by_value;
			$this->load->view('admin/topstories', $data);
 		}
		
		function search_by(){
				
			$query_array = array(
				'search_by'=>$this->input->post('search_by'),
				'search'=>$this->input->post('search')
			);
			$query_id=rand();
			$_SESSION['search']=$query_array;
			//echo '<pre>';print_r($_SESSION['search']);exit;
			redirect('admin/topstories/index/'.$query_id);
			
		}
		
		/* function create_topstories_page(){
			$this->load->view('admin/create_topstories_page');
		} */
		
		function create_topstories(){
			$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('title', 'Title', 'required|alpha|min_length[3]|max_length[50]');
				$this->form_validation->set_rules('date', 'Date', 'required');
				$this->form_validation->set_rules('content', 'Content', 'max_length[225]');
				$this->form_validation->set_rules('author', 'Author', 'required|alpha|min_length[3]|max_length[20]');
				
				if($this->form_validation->run() == FALSE){
						$this->load->view('admin/create_topstories_page');
				}else{
					
					$tmp_name = $_FILES['image']['tmp_name'];
					$image = addslashes($tmp_name);
					$image = file_get_contents($image);				
					$image = base64_encode($image);
					
					//echo '<pre>';print_r($image);exit;
					$url = CURL_URL;
					$data = array('www-command'=>'topstories-create_topstories', 
					'topstories_title' => $this->input->post('title'),
					'topstories_date' => $this->input->post('date'),
					'topstories_content' => $this->input->post('content'),
					'topstories_author' => $this->input->post('author'),
					'topstories_image' => $image,
					'image_name' => $_FILES['image']['name']
					);
					//echo '<pre>';print_r($data);exit;
					$string = http_build_query($data);
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					//echo '<pre>';print_r($response);exit;
					curl_close($ch);
					redirect('admin/topstories/index/');
				}
			}
			
			/* function edit_topstories_page(){
					$url = CURL_URL;
					$data = array('www-command'=>'topstories-get_topstories', 
					'topstories_id' => $this->uri->segment(4)
					);
					//echo '<pre>';print_r($data);exit;
					$string = http_build_query($data);
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					//$data['organizations_get_organization'] = $response;
					//echo '<pre>';print_r($response);exit;
					$response = (array) $response;
					foreach($response as $value){
						$value = $value;
					}
					$data['title'] = $value[0]->topstories_title;
					$data['date'] = $value[0]->topstories_date;
					$data['content'] = $value[0]->topstories_content;
					$data['author'] = $value[0]->topstories_author;
					$data['image'] = $value[0]->topstories_image;
					curl_close($ch);
				$this->load->view('admin/edit_topstories_page', $data);
			} */
			
			function edit_topstories(){
				$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('title', 'Title', 'required|alpha|min_length[3]|max_length[50]');
				$this->form_validation->set_rules('date', 'Date', 'required');
				$this->form_validation->set_rules('content', 'Content', 'max_length[225]');
				$this->form_validation->set_rules('author', 'Author', 'required|alpha|min_length[3]|max_length[20]');
				
				if($this->form_validation->run() == FALSE){
					$url = CURL_URL;
					if($this->uri->segment(4) > 0){
						$_SESSION['storyid'] = $this->uri->segment(4);
					}
					$data = array('www-command'=>'topstories-get_topstories', 
					'topstories_id' => $_SESSION['storyid']
					);
					//echo '<pre>';print_r($data);exit;
					$string = http_build_query($data);
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					//$data['organizations_get_organization'] = $response;
					//echo '<pre>';print_r($response);exit;
					$response = (array) $response;
					foreach($response as $value){
						$value = $value;
					}
					//echo '<pre>';print_r($value);exit;
					$data['title'] = $value[0]->topstories_title;
					$data['date'] = $value[0]->topstories_date;
					$data['content'] = $value[0]->topstories_content;
					$data['author'] = $value[0]->topstories_author;
					$data['image'] = $value[0]->topstories_image;
					curl_close($ch);
					$this->load->view('admin/edit_topstories_page', $data);
					
				}else{
					
					$tmp_name = $_FILES['image']['tmp_name'];
					$image = addslashes($tmp_name);
					$image = file_get_contents($image);				
					$image = base64_encode($image);
					
					$url = CURL_URL;
					$data = array('www-command'=>'topstories-edit_topstories', 
					'topstories_title' => $this->input->post('title'),
					'topstories_date' => $this->input->post('date'),
					'topstories_content' => $this->input->post('content'),
					'topstories_author' => $this->input->post('author'),
					'topstories_image' => base64_encode($image),
					'topstories_id' => $_SESSION['storyid'],
					'image_name' => $_FILES['image']['name']
					);
					//echo '<pre>';print_r($data);exit;
					$string = http_build_query($data);
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					//echo '<pre>';print_r($response);exit;
					curl_close($ch);
					redirect('admin/topstories/index/');
				}
			}
			
			function delete_topstories(){
				$url = 'http://localhost/azindia/azindia-api/json.api';
				if($this->uri->segment(5) == 0){
					$status = 1;
				}else{
					$status = 0;
				}
				$data = array('www-command'=>'topstories-delete_topstories','status'=>$status, 'topstories_id'=>$this->uri->segment(4));
				$string = http_build_query($data);
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);			
				$response = json_decode(curl_exec($ch));
				
				redirect('admin/topstories/index/');
			}
	}
